/********************************************************
* Projet Chat System
* 
* Yann MBOUNGOU et Jean-Christophe VENDOME
* ELEVES INGENIEUR - 4e Année - Informatique et Réseaux
* INSA TOULOUSE, Dept. GEI - 2014 - 2015
*********************************************************/
package GUI;
import Attach.ImageFileView;
import Attach.ImageFilter;
import Attach.ImagePreview;
import Interfaces.FromUser;
import Interfaces.ToUser;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.UnknownHostException;
import javax.swing.*;

public class MsgPrivate extends JFrame implements ActionListener,FromUser, ToUser{
     final JPanel mainPanel,logPanel,privatePanel; 
     private final JButton retour,psend,pFile;
     private final JTextField pftmessage;
     JScrollPane scroll;
     private final JColorTextPane disp;
     String label_msg="MSG :";
     private JFileChooser  fileDialog;
     private File file_joined ;
     private ChatGUI gui;
     private User monu;
     
     @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
    public  MsgPrivate(User u)
    {
    super("chat with "+u.getLogin());
    this.monu=u;
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setSize(400,200);
    mainPanel=new JPanel();
    mainPanel.setPreferredSize(new Dimension(400,200));
    this.add(mainPanel);
   
     /******************  msgPanel  **********************///panel de chat
    logPanel=new JPanel();      
    logPanel.setBackground(new java.awt.Color(204, 204, 255));  
    logPanel.setPreferredSize(new Dimension(400,100));
    disp=new JColorTextPane();   
    disp.setPreferredSize(new Dimension(390,100));
    disp.setEditable(false);
    scroll=new JScrollPane(getDisp());
    logPanel.add(scroll); 
    
    /******************  privatePanel  **********************///panel de message en privé
    privatePanel=new JPanel();
    privatePanel.setBackground(new java.awt.Color(204, 204, 255)); 
    privatePanel.setPreferredSize(new Dimension(400,30));
    privatePanel.add(new JLabel(label_msg));
    pftmessage=new JTextField("",12);
    privatePanel.add(pftmessage);
    psend=new JButton("SEND");
    psend.addActionListener(this);
    privatePanel.add(psend);        
    psend.setBackground(new java.awt.Color(102,102,255));
    psend.setForeground(new java.awt.Color(0, 0, 0));
    pFile = new JButton("File");
    pFile.setBackground(new java.awt.Color(153, 0, 153));
    pFile.setForeground(new java.awt.Color(0, 0, 0));
    pFile.addActionListener(this);
    privatePanel.add(pFile);    
    retour=new JButton("Back");   
    retour.setBackground(new java.awt.Color(0, 255, 255));
    retour.setForeground(new java.awt.Color(0, 0, 0));            
    retour.addActionListener(this);
    privatePanel.add(retour);
    mainPanel.add(privatePanel);
    mainPanel.add(logPanel);
    
    setVisible(true);// En dernier
  }

    public JColorTextPane getDisp() {
        return disp;
    }
    
    public JTextField getPftmessage() {
        return pftmessage;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object obj=e.getSource();
        if (obj instanceof JButton)
        {
           JButton b=(JButton)obj;
           if (b==retour) back();
           else if (b==pFile) openPrivate();
           else if (b==psend)try {
               privsend();
           } catch (UnknownHostException ex) {
               System.err.println("!!ChatPriv: Exception ="+ex);
           } 
        }
    }
    
     @Override
    public void openPrivate()
    {
        if (this.getFileDialog() == null) 
        {
            fileDialog = new JFileChooser();    
            this.getFileDialog().addChoosableFileFilter(new ImageFilter());//(Accept All) file filter.
            this.getFileDialog().setAcceptAllFileFilterUsed(false);//Add a custom file filter and disable the default	    
            this.getFileDialog().setFileView(new ImageFileView());//Add custom icons for file types.	    
            this.getFileDialog().setAccessory(new ImagePreview(this.getFileDialog()));//Add the preview pane.
        }
        int returnVal = this.getFileDialog().showDialog(this,"Join File");
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            file_joined = this.getFileDialog().getSelectedFile();
            this.getDisp().append(java.awt.Color.black,getFile_joined().getName()+ " joined .\n");
        } 
        else 
        {
            this.getDisp().append(java.awt.Color.red,"Attachment cancelled by "+displayLogin()+".\n");
        }
        this.getDisp().setCaretPosition(this.getDisp().getDocument().getLength());        
        this.getFileDialog().setSelectedFile(null);//Reset the file chooser for the next time it's shown.
    }
    
        public void privsend() throws UnknownHostException                                                                                        
    {
        this.getDisp().append(java.awt.Color.pink,displayLogin());
        this.getDisp().append(" : "+this.getPftmessage().getText()+"\n"); 
        for(User u: getGui().getUserList())
            {                   
                String m=""+getGui().getjUserList().getSelectedValue();
                if(u.getLogin().equals(monu.getLogin()))  
                {
                    System.out.println("msg private to "+u);
                    getGui().getChatController().performSendprivate(displayLogin(),this.getPftmessage().getText(),u.getAddr());
                }
                else if(u.getLogin().equals(m))  
                {
                    System.out.println("msg private to "+u);
                    getGui().getChatController().performSendprivate(displayLogin(),this.getPftmessage().getText(),u.getAddr());
                }
            }        
    }
        
    @Override
     public void back()                                                                          
    {  
        disp.setText("");
        this.setVisible(false);
    }  

    @Override
    public void connect() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void disconnect() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public String displayLogin() {
    return this.getGui().getJftlogin().getText();    
    }

    @Override
    public void send() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void open() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void connected() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void disconnected() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void ReceiveMsg(String login, String message) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void displayUserList() {
        this.getGui().getjUserList().setListData(this.getGui().getUserList().toArray());    
    }
    
        //*********************File joinned****************************//
    public File getFile_joined() {
        return file_joined;
    }

    public JFileChooser getFileDialog() {
        return fileDialog;
    }

    public void setGui(ChatGUI gui) {
        this.gui = gui;
    }

    public ChatGUI getGui() {
        return gui;
    }

    /**
     * @return the monu
     */
    public User getMonu() {
        return monu;
    }

    /**
     * @param monu the monu to set
     */
    public void setMonu(User monu) {
        this.monu = monu;
    }
}
